module.exports = function (grunt) {
	var path = require('path');
  	var numCPUs = require('os').cpus().length;
	grunt.registerMultiTask('sass', 'Compile Sass to CSS', function () {
	    var cb = this.async();
	    grunt.util.async.forEachLimit(this.files, numCPUs, function (file, next) {
	     var args = ['--sourcemap=none','--force','--update']
	      var src = file.src[0];
	      var dest = file.dest;
	      args.push(src + ":" + dest);
	      grunt.util.spawn({
	        cmd: 'sass',
	        args: args,
	        opts: {
	          stdio: 'inherit'
	        }
	      }, function (error, result, code) {
	        if (code === 127) {
	          return grunt.warn(
	            'You need to have Ruby and Sass installed and in your PATH for\n' +
	            'this task to work. More info:\n' +
	            'https://github.com/gruntjs/grunt-contrib-sass'
	          );
	        }
	        next(error);
	      });
	    }, cb);
	});
  
   grunt.initConfig({
   		//Read the package.json (optional)
        pkg: grunt.file.readJSON('package.json'),
        // Metadata.
        meta: {
            basePath: '..',
            srcPath: 'dev/sass/',
            deployPath: 'dev/css/'
        },

        banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
                '<%= .template.today("yyyy-mm-dd") %>\n' +
                '* Copyright (c) <%= grunt.template.today("yyyy") %> ',

        // Task configuration.
        sass: {
            compile: {
            	files : {
                     'dev/css':'dev/sass/theme'
                }
            }
        },
        
       template: {
            release: {
                'options': {
                   'data': grunt.file.readJSON('docs/changes/changes.json'),
                   'delimiters': 'handlebars-like-delimiters'
                },
                'files': {
                    'docs/changes/changes.html': ['docs/changes/changes.tpl']
                }
            }
       },
        
        watch: {
            scripts: {
                files: [
                    '<%= meta.srcPath %>/**/*.scss'
                ],
                tasks: ['sass']
            }
        },
	    requirejs: {
		    	dist: {
			        options: {
			            mainConfigFile: 'dev/hteos.js',
			            baseUrl: '..',
						appDir: 'dev',
						dir: 'dist',
						optimize: 'none',
		  				allowSourceOverwrites : true,
						modules: [{
							name: 'dist/hteos'
						}],
						optimizeCss: 'standard',
						removeCombined: true,
						paths: {
							HteOS: 'dev/src'
						},
						fileExclusionRegExp:/^.svn/
			        }
	        	}
		    },
		    jsObfuscate: {
			    obfuscate: {
			      options: {
			        concurrency: 2,
			        keepLinefeeds: false,
			        keepIndentations: false,
			        encodeStrings: true,
			        encodeNumbers: true,
			        moveStrings: true,
			        replaceNames: true,
			        variableExclusions: [ '^_get_', '^_set_', '^_mtd_' ]
			      },
			      files: {
			        'dist/hteos.js': [
			          'dist/hteos.js'
			        ]
			      }
			    }
			  },
			jsduck: {
			    main: {
			        src: ['dev/src'],
			        dest: 'docs/api',
			        options: {
			            'builtin-classes': true,
			            'warnings': ['-no_doc', '-dup_member', '-link_ambiguous'],
			            'footer': "<a href='http://www.hteos.com'>Powered By HteOS</a>",
			            'title': "HteOS JS API文档"
			        }
			    }
			},
			'string-replace': {
			      prod: {
			        src: 'dist/index.html',
			        dest: 'dist/index.html',
			        options: {
			          replacements: [{
			            pattern: '<script src="require.js" data-main="hteos"></script>',
			            replacement: '<script src="hteos.min.js"></script>'
			          }]
			        }
			      }
			    },
		    clean : ['dist/sass','dist/dist','dist/require.js'],
		    rename: {
		        min: {
		            src: 'dist/hteos.js',
		            dest: 'dist/hteos.min.js'
		        }
		    },
		    'ftp-deploy': {
			  deploy: {
			    auth: {
			      host: '45.114.11.84',
			      port: 21,
			      authKey: 'key1',
			      authPath:'D:/hteos/84.ftppass'
			    },
			    src: 'dist',
			    dest: '/cp'
			  }
			}
		});
	grunt.loadNpmTasks('grunt-contrib-requirejs');
	//grunt.loadNpmTasks('grunt-contrib-sass');
	grunt.loadNpmTasks('grunt-string-replace');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-ftp-deploy');
    grunt.loadNpmTasks('js-obfuscator');
    grunt.loadNpmTasks('grunt-jsduck');
    grunt.loadNpmTasks('grunt-rename');
    grunt.loadNpmTasks('grunt-template');
	grunt.registerTask('build', ['sass','requirejs','clean','jsObfuscate','rename','string-replace']);
}